package com.notes.algo.t8;

import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @Describe 字典压缩
 * @date
 */
public class Dic {
    public static void main(String[] args) {
        List<String> dicR = dicCompress("abcdbbccaaabaeaaabaeea");
        dicR.forEach(x -> System.out.println(x));

        /*List<String> dicR2 = dicCompress("ABBC");
        dicR2.forEach(x -> System.out.println(x));*/

        /*List<String> dicR3 = dicCompress("ABCBCBCBCA");
        dicR3.forEach(x -> System.out.println(x));*/
    }

    public static List<String> dicCompress(String dicStr) {
        // System.out.println(dicStr);
        List<String> result = new ArrayList<String>();
        // int maxWidow = 10;
        int hisWindow;
        int preReadWindow;
        int length = dicStr.length();
        for (int i = 0; i < length; i++) {
            /*if (i + 1 <= maxWidow) {
                hisWindow = i;
            } else {
                hisWindow = maxWidow;
            }*/
            hisWindow = i;

            if (i + hisWindow < length) {
                preReadWindow = hisWindow;
            } else {
                preReadWindow = length - i;
            }

            String hisStr = dicStr.substring(i - hisWindow, hisWindow);
            String preReadStr = dicStr.substring(i, i + preReadWindow);
            // System.out.println(hisStr + " " + preReadStr);

            String matchStr = "";
            while (true) {
                if (preReadWindow <= 0) {
                    break;
                }
                String substring = preReadStr.substring(0, preReadWindow);
                if (hisStr.contains(substring)) {
                    matchStr = substring;
                    break;
                }
                preReadWindow--;
                if (preReadWindow <= 0) {
                    break;
                }
            }

            StringBuffer sb = new StringBuffer();
            if (!"".equals(matchStr)) {
                int matchIndex = hisStr.lastIndexOf(matchStr);
                int matchStrLen = matchStr.length();
                int preReadStrLen = preReadStr.length();
                if (matchStrLen < preReadStrLen) {
                    // String nextChar = preReadStr.substring(matchStrLen, matchStrLen + 1);
                    char nextChar = dicStr.charAt(i + matchStr.length());
                    int hisLen = hisStr.length();
                    sb.append("(").append(hisLen - matchIndex).append("," + matchStrLen).append("," + nextChar).append(")");
                    result.add(sb.toString());
                    /*if (i + preReadStrLen < length - 1) {
                        i += (preReadStrLen - 1);
                    } else {
                        i += (matchStrLen - 1);
                    }*/
                    i += preReadWindow;

                    System.out.println(i);
                } else {
                    int hisLen = hisStr.length();
                    sb.append("(").append(hisLen - matchIndex).append("," + matchStrLen).append("," + "").append(")");
                    result.add(sb.toString());
                    break;
                }
            } else {
                char nextChar = dicStr.charAt(i);
                sb.append("(").append(0).append("," + 0).append("," + nextChar).append(")");
                result.add(sb.toString());
            }
            System.out.println(hisStr + " " + preReadStr + " -> " + sb.toString() + " " + i);
        }

        return result;
    }

    public static String dicUnCompress(List<String> dicComStrList) {
        for (int i = 0; i < dicComStrList.size(); i++) {
            String comStr = dicComStrList.get(i);

        }

        return "";
    }
}
